package com.laivi.basic.model.orm;

import java.lang.reflect.Field;

import com.laivi.basic.annotation.orm.Comments;
import com.laivi.basic.annotation.orm.Id;
import com.laivi.basic.annotation.orm.LongText;
import com.laivi.basic.annotation.orm.Table;
import com.laivi.basic.type.orm.GenerateSQLServerColumn;
import com.laivi.basic.util.LangUtil;

public class GenerateSQLServerTable implements IGenerateTable {

	@Override
	public String generateCreate(Class<?> klass) {
		Table table = klass.getAnnotation(Table.class);
		if (null != table) {
			StringBuffer sql = new StringBuffer("create table " + table.value()+ "(");
			for (Field field : LangUtil.getFields(klass)) {
				sql.append(gengerateField(field) + ",");
			}
			sql = new StringBuffer(sql.substring(0, sql.length() - 1));
			sql.append(")");
			return sql.toString();
		}

		return null;
	}

	@Override
	public String generateUpdate(Class<?> klass) {
		return null;
	}

	public String gengerateField(Field field) {
		StringBuffer fields = new StringBuffer();
		Id id = field.getAnnotation(Id.class);
		LongText longText = field.getAnnotation(LongText.class);
		if (field.getType().isEnum()) {
			fields.append(field.getName() + " nvarchar(30) DEFAULT NULL");
		} else if (null != longText) {
			fields.append(field.getName() + " text DEFAULT NULL");
		} else if (null != id) {
			fields.append(field.getName()
					+ " "
					+ GenerateSQLServerColumn.fromType(
							field.getType().toString()).toColumn()
					+ " NOT NULL IDENTITY(1,1)");
			fields.append(",PRIMARY KEY (" + field.getName() + ")");
		} else {
			fields.append(field.getName()
					+ " "
					+ GenerateSQLServerColumn.fromType(
							field.getType().toString()).toColumn()
					+ " default NULL");
		}
		return fields.toString();
	}

	@Override
	public String generateComments(Class<?> klass) {
		Table table = klass.getAnnotation(Table.class);
		if (null != table) {
			StringBuffer sql = new StringBuffer();
			for (Field field : LangUtil.getFields(klass)) {
				sql.append(generateCommentField(field,table.value()));
			}
			return sql.toString();
		}
		return null;
	}

	public String generateCommentField(Field field, String table) {
		StringBuffer sb = new StringBuffer();
		Comments comments = field.getAnnotation(Comments.class);
		if (null != comments) {
			sb.append(" IF ((SELECT COUNT(*) from fn_listextendedproperty('MS_Description','SCHEMA', N'dbo','TABLE', N'"
					+ table + "','COLUMN', N'" + field.getName() + "')) > 0)");
			sb.append(" EXEC sp_updateextendedproperty @name = N'MS_Description', @value =N'"+ comments.value()
					+ "', @level0type = 'SCHEMA', @level0name = N'dbo', @level1type = 'TABLE', @level1name = N'"+ table
					+ "', @level2type = 'COLUMN', @level2name = N'"+ field.getName() + "'");
			sb.append(" ELSE EXEC sp_addextendedproperty @name = N'MS_Description', @value =N'"+ comments.value()
					+ "', @level0type = 'SCHEMA', @level0name = N'dbo', @level1type = 'TABLE', @level1name = N'"+ table
					+ "', @level2type = 'COLUMN', @level2name = N'"+ field.getName() + "'");
		}
		return sb.toString();
	}

}
